package com.farm.cbtg.service.impl;

import com.farm.cbtg.domain.Appcatalog;
import com.farm.cbtg.domain.ex.CatalogDomain;
import com.farm.core.time.TimeTool;
import org.apache.log4j.Logger;
import com.farm.cbtg.dao.AppcatalogDaoInter;
import com.farm.cbtg.service.AppcatalogServiceInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBRuleList;
import com.farm.core.sql.query.DataQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import javax.annotation.Resource;
import com.farm.core.auth.domain.LoginUser;

/* *
 *功能：业务分类服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@Service
public class AppcatalogServiceImpl implements AppcatalogServiceInter {
	@Resource
	private AppcatalogDaoInter appcatalogDaoImpl;

	private static final Logger log = Logger.getLogger(AppcatalogServiceImpl.class);

	@Override
	@Transactional
	public Appcatalog insertAppcatalogEntity(Appcatalog entity, LoginUser user) {
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setMuser(user.getId());
		entity.setUtime(TimeTool.getTimeDate14());
		entity.setState("1");
		if (entity.getParentid() == null) {
			entity.setParentid("NONE");
		}
		entity.setTreecode("NONE");
		entity = appcatalogDaoImpl.insertEntity(entity);
		initTreeCode(entity.getId());
		return entity;
	}

	private void initTreeCode(String treeNodeId) {
		Appcatalog node = getAppcatalogEntity(treeNodeId);
		if (node.getParentid().equals("NONE")) {
			node.setTreecode(node.getId());
		} else {
			node.setTreecode(appcatalogDaoImpl.getEntity(node.getParentid()).getTreecode() + node.getId());
		}
		appcatalogDaoImpl.editEntity(node);
	}

	@Override
	@Transactional
	public Appcatalog editAppcatalogEntity(Appcatalog entity, LoginUser user) {
		Appcatalog entity2 = appcatalogDaoImpl.getEntity(entity.getId());
		entity2.setMuser(user.getId());
		entity2.setSort(entity.getSort());
		entity2.setState(entity.getState());
		entity2.setUtime(TimeTool.getTimeDate14());
		entity2.setComments(entity.getComments());
		entity2.setName(entity.getName());
		entity2.setId(entity.getId());
		appcatalogDaoImpl.editEntity(entity2);
		return entity2;
	}

	@Override
	@Transactional
	public void deleteAppcatalogEntity(String id, LoginUser user) {
		{
			List<Appcatalog> types = appcatalogDaoImpl
					.selectEntitys(DBRuleList.getInstance().add(new DBRule("PARENTID", id, "=")).toList());
			if (types.size() > 0) {
				throw new RuntimeException("分类下还有分类，请先删除子分类！");
			}
		}
		appcatalogDaoImpl.deleteEntity(appcatalogDaoImpl.getEntity(id));
	}

	@Override
	@Transactional
	public Appcatalog getAppcatalogEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return appcatalogDaoImpl.getEntity(id);
	}

	@Override
	@Transactional
	public DataQuery createAppcatalogSimpleQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		DataQuery dbQuery = DataQuery.init(query, "CBTG_APP_CATALOG",
				"ID,TREECODE,COMMENTS,NAME,CTIME,UTIME,STATE,MUSER,PARENTID,SORT,DOMAIN");
		return dbQuery;
	}

	@Override
	@Transactional
	public void moveTreeNode(String orgId, String targetOrgId, LoginUser currentUser) {
		String[] orgIds = orgId.split(",");
		for (int i = 0; i < orgIds.length; i++) {
			// 移动节点
			Appcatalog node = getAppcatalogEntity(orgIds[i]);
			// if (node.getParentid().equals("NONE")) {
			// throw new RuntimeException("不能够移动根节点!");
			// }
			if (targetOrgId.equals("NONE")) {
				node.setParentid("NONE");
			} else {
				Appcatalog target = getAppcatalogEntity(targetOrgId);
				if (target.getTreecode().indexOf(node.getTreecode()) >= 0) {
					throw new RuntimeException("不能够移动到其子节点下!");
				}
				node.setParentid(targetOrgId);
			}
			appcatalogDaoImpl.editEntity(node);
			// 构造所有树TREECODE
			List<Appcatalog> list = appcatalogDaoImpl.getAllSubNodes(orgIds[i]);
			for (Appcatalog type : list) {
				initTreeCode(type.getId());
			}
		}
	}

	@Override
	@Transactional
	public List<Appcatalog> getCatalogs(CatalogDomain domain) {
		return appcatalogDaoImpl
				.selectEntitys(DBRuleList.getInstance().add(new DBRule("DOMAIN", domain.getKey(), "=")).toList());
	}
}
